package org.backend.controller;


import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.backend.pojo.entity.Favor;
import org.backend.pojo.entity.Product;
import org.backend.pojo.vo.FavorWithProductVO;
import org.backend.pojo.vo.ProductCategoriesVO;
import org.backend.pojo.vo.ProductForPayVO;
import org.backend.service.ProductService;
import org.backend.utils.ApiResponse;
import org.backend.utils.JwtUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

import static org.backend.utils.JwtUtil.setOpenId;

@RestController
@RequestMapping("/products")
@Slf4j
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 获取所有商品列表
     *
     * @return
     */
    @PostMapping("/allProList")
    public ApiResponse<List<Product>> getAllProList() {
        return productService.getAllProList();
    }

    /**
     * 根据用户id查询对应的商品，如果type为full，则查询所有字段，否则查询对应的图像数据
     *
     * @param requestData
     * @return
     */
    @PostMapping("/proList")
    public ApiResponse<?> findAll(@RequestBody Map<String, String> requestData, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String openid = JwtUtil.getOpenId(token);
        return productService.findAll(requestData.get("userId"), openid, requestData.get("type"));
    }


    /**
     * 用户发布商品
     *
     * @param product
     * @return
     */
    @PostMapping("/publish")
    public ResponseEntity<String> publish(@RequestBody Product product, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        product = setOpenId(product, token);
        return productService.publish(product);
    }

    /**
     * 删除
     *
     * @param product
     * @return
     */
    @PostMapping("/delete")
    public ResponseEntity<String> delete(@RequestBody Product product) {
        return productService.delete(product);
    }

    /**
     * 修改
     *
     * @param product
     * @return
     */
    @PostMapping("/revise")
    public ResponseEntity<String> revise(@RequestBody Product product) {
        return productService.revise(product);
    }

    /**
     * 随机查找4个热门商品
     *
     * @return
     */
    @PostMapping("/getPopularProducts")
    public ApiResponse<List<Product>> getPopularProducts() {
        return productService.getPopularProducts();
    }

    /**
     * 根据商品id查询商品
     *
     * @param product
     * @return
     */
    @PostMapping("/getProductById")
    public ApiResponse<Product> getProductById(@RequestBody Product product) {
        return productService.getProductById(product.getProductId());
    }

    /**
     * 获取订单支付时的数据，添加了数量字段
     *
     * @param product
     * @return
     */
    @PostMapping("/getProductByIdForPay")
    public ApiResponse<ProductForPayVO> getProductByIdForPay(@RequestBody Product product) {
        Product product1 = productService.getProductById(product.getProductId()).getData();
        ProductForPayVO productForPayVO = new ProductForPayVO();
        BeanUtils.copyProperties(product1, productForPayVO);
        return ApiResponse.success(productForPayVO);
    }

    /**
     * 统计分类种类
     *
     * @return
     */
    @PostMapping("/getProductCategories")
    public ApiResponse<List<ProductCategoriesVO>> getProductCategories() {
        return productService.getProductCategories();
    }

    /**
     * 随机获取4个商品类别
     *
     * @return
     */
    @PostMapping("/getRandomCategories")
    public ApiResponse<List<ProductCategoriesVO>> getRandomCategories() {
        return productService.getRandomCategories();
    }


    /**
     * 获取某个用户单个物品是否已经被收藏
     *
     * @param favor
     * @param request
     * @return
     */
    @PostMapping("/getFavor")
    public ApiResponse<Boolean> isFavor(@RequestBody Favor favor, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        favor = setOpenId(favor, token);
        return productService.getFavor(favor);
    }

    /**
     * 根据收藏状态，收藏或取消收藏
     *
     * @param favor
     * @param request
     * @return
     */
    @PostMapping("/editFavor")
    public ApiResponse<String> editFavor(@RequestBody Favor favor, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        favor = setOpenId(favor, token);
        return productService.editFavor(favor);
    }

    /**
     * 获取收藏列表
     *
     * @param favor
     * @param request
     * @return
     */
    @PostMapping("/getAllFavor")
    public ApiResponse<List<FavorWithProductVO>> getAllFavor(@RequestBody Favor favor, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        favor = setOpenId(favor, token);
        return productService.getAllFavor(favor);
    }

}
